<?php use PHPCI\Helper\Lang; ?>

<?php if (isset($_GET['saved']) && $_GET['saved'] == 1): ?>
<p class="alert alert-success" style="margin-bottom: 20px;">
    <?php Lang::out('settings_saved'); ?>
</p>
<?php endif; ?>

<?php if (isset($_GET['saved']) && $_GET['saved'] == 2): ?>
<p class="alert alert-danger" style="margin-bottom: 20px;">
    <?php Lang::out('settings_check_perms'); ?>
</p>
<?php endif; ?>

<?php if (!$isWriteable): ?>
    <p class="alert alert-danger" style="margin-bottom: 20px;">
        <?php Lang::out('settings_cannot_write'); ?>
    </p>
<?php endif; ?>

<?php if (isset($_GET['linked']) && $_GET['linked'] == 1): ?>
    <p class="alert alert-success" style="margin-bottom: 20px;">
        <?php Lang::out('settings_github_linked'); ?>
    </p>
<?php endif; ?>

<?php if (isset($_GET['linked']) && $_GET['linked'] == 2): ?>
    <p class="alert alert-danger" style="margin-bottom: 20px;">
        <?php Lang::out('settings_github_not_linked'); ?>
    </p>
<?php endif; ?>

<div class="box box-primary">
    <div class="box-body clearfix">
        <?php print $basicSettings; ?>
    </div>
</div>

<div class="box box-primary">
    <div class="box-header"><h3 class="box-title"><?php Lang::out('latest_builds'); ?></h3></div>
    <div class="box-body clearfix">
        <?php print $latestBuildsSettings; ?>
    </div>
</div>

<div class="box box-primary">
    <div class="box-header"><h3 class="box-title"><?php Lang::out('build_settings'); ?></h3></div>
    <div class="box-body clearfix">
        <?php print $buildSettings; ?>
    </div>
</div>

<div class="box box-primary">
    <div class="box-header"><h3 class="box-title"><?php Lang::out('github_application'); ?></h3></div>
    <div class="box-body clearfix">

        <div class="row">
            <div class="col-lg-12">
                <?php
                $id = null;

                if (isset($settings['phpci']['github']['id'])) {
                    $id = $settings['phpci']['github']['id'];
                }

                $returnTo = PHPCI_URL . 'settings/github-callback';
                $githubUri = 'https://github.com/login/oauth/authorize?client_id='.$id.'&scope=repo&redirect_uri=' . $returnTo;
                ?>
                <?php if (!empty($id)): ?>
                    <?php if (empty($githubUser['name']) || empty($settings['phpci']['github']['token'])): ?>
                        <p class="alert alert-warning clearfix">
                            <?php Lang::out('github_sign_in', $githubUri); ?>
                        </p>
                    <?php else: ?>
                        <p class="alert alert-success">
                            <?php Lang::out('github_phpci_linked'); ?>

                            <strong>
                                <a href="<?php echo $githubUser['html_url']; ?>"><?php echo $githubUser['name']; ?></a>
                            </strong>
                        </p>
                    <?php endif; ?>
                <?php endif; ?>
            </div>

            <div class="col-lg-8">
                <?php print $github; ?>
            </div>

            <div class="col-lg-4">
                <div class="box box-info">
                    <div class="box-header">
                        <h3 class="box-title"><?php Lang::out('github_where_to_find'); ?></h3>
                    </div>

                    <div class="box-body">
                        <p>
                            <?php Lang::out('github_where_help'); ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>


<div class="box box-primary">
    <div class="box-header">
        <h3 class="box-title"><?php Lang::out('email_settings'); ?></h3>
    </div>

    <div class="box-body clearfix">
        <?php if (!isset($settings['phpci']['email_settings'])): ?>
            <p class="alert alert-warning clearfix">
                <?php Lang::out('email_settings_help'); ?>
            </p>
        <?php endif; ?>

        <?php print $emailSettings; ?>

    </div>
</div>

<div class="box box-primary">
    <div class="box-header">
        <h3 class="box-title">Authentication Settings</h3>
    </div>

    <div class="box-body clearfix">
        <p class="alert alert-warning clearfix">
            Be careful: This setting disables authentication and uses your current admin account for all actions within phpci with admin rights.
        </p>
        
        <div class="col-md-12">
            <?php print $authenticationSettings; ?>
        </div>
    </div>
</div>
